﻿Public Class frmSubGrid
    Private m_Receptor As CAermod.Receptor

    Property Receptor() As CAermod.Receptor
        Get
            Return m_Receptor
        End Get
        Set(ByVal value As CAermod.Receptor)
            m_Receptor = value
        End Set
    End Property
    ''' <summary>
    ''' 添加网格
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        Me.m_Receptor.nSubGrid += 1
        Me.ListSubGrid.Items.Add("SubG" & Me.m_Receptor.nSubGrid)
        ReDim Preserve Me.m_Receptor.SubGridCart(Me.m_Receptor.SubGridCart.Length)
        Me.m_Receptor.SubGridCart(Me.m_Receptor.SubGridCart.Length - 1) = New CAermod.GridCart
        Me.m_Receptor.SubGridCart(Me.m_Receptor.SubGridCart.Length - 1).Netid = "SubG" & Me.m_Receptor.nSubGrid
        Me.m_Receptor.SubGridCart(Me.m_Receptor.SubGridCart.Length - 1).SetGrid(-2000, 41, 50, -2000, 41, 50)
        Me.ListSubGrid.SelectedIndex = Me.ListSubGrid.Items.Count - 1

    End Sub

    Private Sub cmdDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDel.Click
        Dim Index As Integer = Me.ListSubGrid.SelectedIndex
        If Index > 0 Then
            If Index < Me.ListSubGrid.Items.Count - 1 Then
                For i As Integer = Index To Me.m_Receptor.SubGridCart.Length - 2
                    Me.m_Receptor.SubGridCart(i) = Me.m_Receptor.SubGridCart(i + 1)
                Next
                ReDim Preserve Me.m_Receptor.SubGridCart(Me.m_Receptor.SubGridCart.Length - 2)
                Me.ListSubGrid.Items.Remove(Me.ListSubGrid.SelectedItem)
                Me.ListSubGrid.SelectedIndex = Index
            Else
                ReDim Preserve Me.m_Receptor.SubGridCart(Me.m_Receptor.SubGridCart.Length - 2)
                Me.ListSubGrid.Items.Remove(Me.ListSubGrid.SelectedItem)
                Me.ListSubGrid.SelectedIndex = Index - 1
            End If
        ElseIf Index = 0 Then
            MessageBox.Show("不能删除主网格!")
        End If
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.Close()
    End Sub

    Private Sub ListSubGrid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListSubGrid.SelectedIndexChanged
        Me.m_Receptor.SelectSubGridIndex = Me.ListSubGrid.SelectedIndex

        RefreshElex()
        chkFlagSelected_CheckedChanged(sender, e)
    End Sub
    '用对象中的数据更新表格和窗体
    Private Sub RefreshElex()
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            tabGrid.Enabled = True
            Me.chkGrid.Checked = Me.m_Receptor.SubGridCart(index).Selected
            Me.txtNetID.Text = Me.m_Receptor.SubGridCart(index).Netid '编号
            Me.txtXinit.Text = Me.m_Receptor.SubGridCart(index).Xinit 'X轴的开始坐标
            Me.txtXnum.Text = Me.m_Receptor.SubGridCart(index).Xnum 'X轴的个数
            Me.txtXdelta.Text = Me.m_Receptor.SubGridCart(index).Xdelta 'X轴的步长
            Me.txtYinit.Text = Me.m_Receptor.SubGridCart(index).Yinit
            Me.txtYnum.Text = Me.m_Receptor.SubGridCart(index).Ynum
            Me.txtYdelta.Text = Me.m_Receptor.SubGridCart(index).Ydelta
            Me.chkFlagSelected.Checked = Me.m_Receptor.SubGridCart(index).FlagSelected

            '设置ELEV表格的行、列数
            Me.FlexElev.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexElev.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置ELEV表格的数据
            For i As Integer = 1 To Me.FlexElev.Cols.Count - 1
                Me.FlexElev.SetData(0, i, Me.m_Receptor.SubGridCart(index).Xinit + (i - 1) * Me.m_Receptor.SubGridCart(index).Xdelta)
            Next
            For i As Integer = 1 To Me.FlexElev.Rows.Count - 1
                Me.FlexElev.SetData(Me.FlexElev.Rows.Count - 1 - (i - 1), 0, Me.m_Receptor.SubGridCart(index).Yinit + (i - 1) * Me.m_Receptor.SubGridCart(index).Ydelta)
            Next
            For i As Integer = 1 To Me.FlexElev.Rows.Count - 1
                For j As Integer = 1 To Me.FlexElev.Cols.Count - 1
                    Me.FlexElev.SetData(Me.FlexElev.Rows.Count - 1 - (i - 1), j, Me.m_Receptor.SubGridCart(index).GetElev(i - 1, j - 1))
                Next
            Next
            Me.FlexElev.AutoSizeCols()

            '设置Hill表格的行、列数
            Me.FlexHill.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexHill.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置Hill表格的数据
            For i As Integer = 1 To Me.FlexHill.Cols.Count - 1
                Me.FlexHill.SetData(0, i, Me.m_Receptor.SubGridCart(index).Xinit + (i - 1) * Me.m_Receptor.SubGridCart(index).Xdelta)
            Next
            For i As Integer = 1 To Me.FlexHill.Rows.Count - 1
                Me.FlexHill.SetData(Me.FlexHill.Rows.Count - 1 - (i - 1), 0, Me.m_Receptor.SubGridCart(index).Yinit + (i - 1) * Me.m_Receptor.SubGridCart(index).Ydelta)
            Next
            For i As Integer = 1 To Me.FlexHill.Rows.Count - 1
                For j As Integer = 1 To Me.FlexHill.Cols.Count - 1
                    Me.FlexHill.SetData(Me.FlexHill.Rows.Count - 1 - (i - 1), j, Me.m_Receptor.SubGridCart(index).GetHill(i - 1, j - 1))
                Next
            Next
            Me.FlexHill.AutoSizeCols()


            '设置Flag表格的行、列数
            Me.FlexFlag.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexFlag.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置Flag表格的数据
            For i As Integer = 1 To Me.FlexFlag.Cols.Count - 1
                Me.FlexFlag.SetData(0, i, Me.m_Receptor.SubGridCart(index).Xinit + (i - 1) * Me.m_Receptor.SubGridCart(index).Xdelta)
            Next
            For i As Integer = 1 To Me.FlexFlag.Rows.Count - 1
                Me.FlexFlag.SetData(Me.FlexFlag.Rows.Count - 1 - (i - 1), 0, Me.m_Receptor.SubGridCart(index).Yinit + (i - 1) * Me.m_Receptor.SubGridCart(index).Ydelta)
            Next
            For i As Integer = 1 To Me.FlexFlag.Rows.Count - 1
                For j As Integer = 1 To Me.FlexFlag.Cols.Count - 1
                    Me.FlexFlag.SetData(Me.FlexFlag.Rows.Count - 1 - (i - 1), j, Me.m_Receptor.SubGridCart(index).GetFlag(i - 1, j - 1))
                Next
            Next
            Me.FlexFlag.AutoSizeCols()
        Else
            tabGrid.Enabled = False
        End If

    End Sub

    Private Sub chkGrid_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkGrid.CheckedChanged
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Selected = Me.chkGrid.Checked
        End If

    End Sub

    Private Sub chkFlagSelected_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFlagSelected.CheckedChanged
        tabGrid.Tabs(3).Visible = chkFlagSelected.Checked

    End Sub

    Private Sub txtNetID_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtNetID.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Netid = txtNetID.Text
        End If

    End Sub

    Private Sub txtXinit_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtXinit.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Xinit = txtXinit.Text
        End If
    End Sub


    Private Sub SetElev()
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Xnum = txtXnum.Text
            '设置ELEV表格的行、列数
            Me.FlexElev.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexElev.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置ELEV的数据
            For i As Integer = 1 To Me.FlexElev.Rows.Count - 1
                For j As Integer = 1 To Me.FlexElev.Cols.Count - 1
                    Me.m_Receptor.SubGridCart(index).SetElev(i - 1, j - 1, Me.FlexElev.GetData(Me.FlexElev.Rows.Count - 1 - (i - 1), j))
                Next
            Next

        End If

        Me.FlexElev.AutoSizeCols()
    End Sub

    Private Sub SetHill()
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            '设置Hill表格的行、列数
            Me.FlexHill.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexHill.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置Hill的数据
            For i As Integer = 1 To Me.FlexHill.Rows.Count - 1
                For j As Integer = 1 To Me.FlexHill.Cols.Count - 1
                    Me.m_Receptor.SubGridCart(index).SetHill(i - 1, j - 1, Me.FlexHill.GetData(Me.FlexHill.Rows.Count - 1 - (i - 1), j))
                Next
            Next
        End If


        Me.FlexHill.AutoSizeCols()
    End Sub
    Private Sub SetFlag()
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            '设置Flag表格的行、列数
            Me.FlexFlag.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexFlag.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置Flag的数据
            For i As Integer = 1 To Me.FlexFlag.Rows.Count - 1
                For j As Integer = 1 To Me.FlexFlag.Cols.Count - 1
                    Me.m_Receptor.SubGridCart(index).SetFlag(i - 1, j - 1, Me.FlexFlag.GetData(Me.FlexFlag.Rows.Count - 1 - (i - 1), j))
                Next
            Next
            Me.FlexFlag.AutoSizeCols()
        End If
    End Sub

    Private Sub txtXnum_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtXnum.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Xnum = txtXnum.Text
            Me.m_Receptor.SubGridCart(index).Ynum = txtYnum.Text
            Me.m_Receptor.SubGridCart(index).Selected = Me.chkGrid.Checked
            Me.m_Receptor.SubGridCart(index).Netid = Me.txtNetID.Text '编号
            Me.m_Receptor.SubGridCart(index).SetGrid(Me.txtXinit.Text, Me.txtXnum.Text, Me.txtXdelta.Text, Me.txtYinit.Text, Me.txtYnum.Text, Me.txtYdelta.Text)
            Me.m_Receptor.SubGridCart(index).FlagSelected = Me.chkFlagSelected.Checked
            '设置Elev数据
            SetElev()
            '设置Hill数据
            SetHill()
            '设置Flag数据
            SetFlag()
           
        End If
    End Sub

    Private Sub txtXdelta_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtXdelta.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Xdelta = txtXdelta.Text
        End If
    End Sub

    Private Sub txtYinit_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtYinit.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Yinit = txtYinit.Text
        End If
    End Sub

    Private Sub txtYnum_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtYnum.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Xnum = txtXnum.Text
            Me.m_Receptor.SubGridCart(index).Ynum = txtYnum.Text
            Me.m_Receptor.SubGridCart(index).Selected = Me.chkGrid.Checked
            Me.m_Receptor.SubGridCart(index).Netid = Me.txtNetID.Text '编号
            Me.m_Receptor.SubGridCart(index).SetGrid(Me.txtXinit.Text, Me.txtXnum.Text, Me.txtXdelta.Text, Me.txtYinit.Text, Me.txtYnum.Text, Me.txtYdelta.Text)
            Me.m_Receptor.SubGridCart(index).FlagSelected = Me.chkFlagSelected.Checked
            '设置Elev数据
            SetElev()
            '设置Hill数据
            SetHill()
            '设置Flag数据
            SetFlag()
        End If
    End Sub

    Private Sub txtYdelta_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtYdelta.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Ydelta = txtYdelta.Text
        End If
    End Sub

    Private Sub FlexElev_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles FlexElev.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            Me.m_Receptor.SubGridCart(index).Xnum = txtXnum.Text
            '设置ELEV表格的行、列数
            Me.FlexElev.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexElev.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置ELEV的数据
            For i As Integer = 1 To Me.FlexElev.Rows.Count - 1
                For j As Integer = 1 To Me.FlexElev.Cols.Count - 1
                    Me.m_Receptor.SubGridCart(index).SetElev(i - 1, j - 1, Me.FlexElev.GetData(Me.FlexElev.Rows.Count - 1 - (i - 1), j))
                Next
            Next
            Me.FlexElev.AutoSizeCols()
        End If
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabGrid.SelectedTabChanged
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            If Me.txtXnum.Text IsNot Nothing AndAlso Me.txtXnum.Text <> "" _
              And Me.txtYnum.Text IsNot Nothing AndAlso Me.txtYnum.Text <> "" Then
                '根据用户的设置相应的设置表格的行列
                Me.FlexElev.Cols.Count = Me.txtXnum.Text + 1
                Me.FlexElev.Rows.Count = Me.txtYnum.Text + 1
                Me.FlexHill.Cols.Count = Me.txtXnum.Text + 1
                Me.FlexHill.Rows.Count = Me.txtYnum.Text + 1
                Me.FlexFlag.Cols.Count = Me.txtXnum.Text + 1
                Me.FlexFlag.Rows.Count = Me.txtYnum.Text + 1
                '设置ELEV表格的数据
                For i As Integer = 1 To Me.FlexElev.Cols.Count - 1
                    Me.FlexElev.SetData(0, i, Me.txtXinit.Text + (i - 1) * txtXdelta.Text)
                Next
                For i As Integer = 1 To Me.FlexElev.Rows.Count - 1
                    Me.FlexElev.SetData(Me.FlexElev.Rows.Count - 1 - (i - 1), 0, txtYinit.Text + (i - 1) * txtYdelta.Text)
                Next
                For i As Integer = 1 To Me.FlexHill.Rows.Count - 1
                    For j As Integer = 1 To Me.FlexHill.Cols.Count - 1
                        If CStr(Me.FlexElev.GetData(Me.FlexHill.Rows.Count - 1 - (i - 1), j)) = "" Then '如果为空，就用0填充
                            Me.FlexElev.SetData(Me.FlexHill.Rows.Count - 1 - (i - 1), j, 0)
                        End If
                    Next
                Next
                '设置Hill表格的数据
                For i As Integer = 1 To Me.FlexHill.Cols.Count - 1
                    Me.FlexHill.SetData(0, i, Me.txtXinit.Text + (i - 1) * txtXdelta.Text)
                Next
                For i As Integer = 1 To Me.FlexHill.Rows.Count - 1
                    Me.FlexHill.SetData(Me.FlexHill.Rows.Count - 1 - (i - 1), 0, txtYinit.Text + (i - 1) * txtYdelta.Text)
                Next
                For i As Integer = 1 To Me.FlexHill.Rows.Count - 1
                    For j As Integer = 1 To Me.FlexHill.Cols.Count - 1
                        If CStr(Me.FlexHill.GetData(Me.FlexHill.Rows.Count - 1 - (i - 1), j)) = "" Then
                            Me.FlexHill.SetData(Me.FlexHill.Rows.Count - 1 - (i - 1), j, 0)
                        End If
                    Next
                Next
                '设置Flag表格的数据
                For i As Integer = 1 To Me.FlexFlag.Cols.Count - 1
                    Me.FlexFlag.SetData(0, i, Me.txtXinit.Text + (i - 1) * txtXdelta.Text)
                Next
                For i As Integer = 1 To Me.FlexFlag.Rows.Count - 1
                    Me.FlexFlag.SetData(Me.FlexFlag.Rows.Count - 1 - (i - 1), 0, txtYinit.Text + (i - 1) * txtYdelta.Text)
                Next
                For i As Integer = 1 To Me.FlexFlag.Rows.Count - 1
                    For j As Integer = 1 To Me.FlexFlag.Cols.Count - 1
                        If CStr(Me.FlexFlag.GetData(Me.FlexFlag.Rows.Count - 1 - (i - 1), j)) = "" Then
                            Me.FlexFlag.SetData(Me.FlexFlag.Rows.Count - 1 - (i - 1), j, 0)
                        End If
                    Next
                Next
                Me.FlexElev.AutoSizeCols()
                Me.FlexHill.AutoSizeCols()
                Me.FlexFlag.AutoSizeCols()
            End If
        End If
    End Sub

    Private Sub FlexHill_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles FlexHill.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            '设置Hill表格的行、列数
            Me.FlexHill.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexHill.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置Hill的数据
            For i As Integer = 1 To Me.FlexHill.Rows.Count - 1
                For j As Integer = 1 To Me.FlexHill.Cols.Count - 1
                    Me.m_Receptor.SubGridCart(index).SetHill(i - 1, j - 1, Me.FlexHill.GetData(Me.FlexHill.Rows.Count - 1 - (i - 1), j))
                Next
            Next
            Me.FlexHill.AutoSizeCols()
        End If
    End Sub

    Private Sub FlexFlag_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles FlexFlag.Validating
        Dim index As Integer = ListSubGrid.SelectedIndex
        If index >= 0 Then
            '设置Flag表格的行、列数
            Me.FlexFlag.Cols.Count = Me.m_Receptor.SubGridCart(index).Xnum + 1
            Me.FlexFlag.Rows.Count = Me.m_Receptor.SubGridCart(index).Ynum + 1
            '设置Flag的数据
            For i As Integer = 1 To Me.FlexFlag.Rows.Count - 1
                For j As Integer = 1 To Me.FlexFlag.Cols.Count - 1
                    Me.m_Receptor.SubGridCart(index).SetFlag(i - 1, j - 1, Me.FlexFlag.GetData(Me.FlexFlag.Rows.Count - 1 - (i - 1), j))
                Next
            Next
            Me.FlexFlag.AutoSizeCols()
        End If
    End Sub

    Private Sub frmSubGrid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FlexElev.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.FlexFlag.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.FlexHill.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)

        ListSubGrid.Items.Clear()
        If Me.m_Receptor.SubGridCart IsNot Nothing Then
            For isub As Integer = 0 To Me.m_Receptor.SubGridCart.Length - 1
                ListSubGrid.Items.Add(Me.m_Receptor.SubGridCart(isub).Netid)
            Next
        End If
        Me.ListSubGrid.SelectedIndex = Me.m_Receptor.SelectSubGridIndex
        ListSubGrid_SelectedIndexChanged(sender, e)


        Me.FlexElev.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.FlexElev.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.FlexElev.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

        Me.FlexFlag.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.FlexFlag.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.FlexFlag.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

        Me.FlexHill.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.FlexHill.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.FlexHill.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None
    End Sub

    Private Sub ListSubGrid_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ListSubGrid.Validating
 
    End Sub
End Class